/**
 * Created by tianling on 2017/3/20.
 */
module.exports = class {
  constructor() {
  }
  static init() {
    return new this();
  }

  /**
   * 根据用户ID获取用户信息
   * @param userId 用户ID
   * @returns {Promise<Model>}
   */
  async getUserInfo(userId) {
    let UserInfo = await global.DataBase.iframe_user.Access.findOne({
      where: {
        UserID: userId
      },
      raw: true
    });
    if (!UserInfo) {
      UserInfo = {
        UserID: 0,
        UserRealName: "",
        UserRule: 0,
        UserOrgStructID: 0,
        UserAccessToken: ""

      };
    } else {
      delete UserInfo.UserPwd;
      if (UserInfo.UserRule == '-9999') {
        if (UserInfo.UserPartnersID === -1) {
          UserInfo.RuleDataDegreeCode = '0101'; //平台所有
        } else {
          UserInfo.RuleDataDegreeCode = '0102'; //本企业全部
        }

      } else {
        let ruleList = UserInfo.UserRule.split(',');
        let RuleDataDegreeCodes = [];
        for (let i = 0; i < ruleList.length; i++) {
          let _rule = await  global.DataBase.iframe_rule.Access.findById(ruleList[i]);
          if (_rule) {
            RuleDataDegreeCodes.push(_rule.RuleDataDegreeCode);
          }
        }
        UserInfo.RuleDataDegreeCode = RuleDataDegreeCodes.join(',');

      }
      if (UserInfo.RuleDataDegreeCode == '0101') {
        UserInfo.IsShowAll = true;
      } else {
        UserInfo.IsShowAll = false;
      }

    }
    return UserInfo;
  };

  /**
   * 获取用户权限
   * @param userRule 用户信息中的UserRule
   * @param partnersId 企业ID
   * @returns {Promise<*>}
   */
  async getUserRuleAble(userRule, partnersId) {
    var systemModule = require("./system-module").init();
    var ret = await systemModule.getRuleAble(userRule, partnersId);
    return ret;
  };

  /**
   * 获取用户可以使用的模块
   * @param userRule 用户信息中的UserRule
   * @param partnersId 企业ID
   * @returns {Promise<*>}
   */
  async getUserModule(userRule, partnersId) {
    var systemModule = require("./system-module").init();
    var ret = await systemModule.getUserModule(userRule, partnersId);
    return ret;
  };


  /**
   * 获取用户在当前页面的权限
   * @param Url
   * @param userRule 用户信息中的UserRule
   * @param partnersId  企业ID
   * @returns {Promise<*>}
   */
  async getPageRule(Url, userRule, partnersId) {
    var CurPageRule = null;
    var systemModule = require("./system-module").init();
    var UserRuleAble = await getUserRuleAble(userRule, partnersId);
    var SystemModule = await systemModule.getSystemModule(false);
    var Module = systemModule.GetModuleByUrl(SystemModule, Url);

    if (Module) {
      for (var i in UserRuleAble) {
        if (UserRuleAble[i].M == Module.ModuleID) {
          CurPageRule = UserRuleAble[i];
          break;
        }
      }
    }

    return CurPageRule;
  };

  /**
   * 判断用户是否登录
   * @param req
   * @param CB
   * @returns {Promise<boolean>}
   */
  async checkLogin(req) {
    let userInfo = await getUserInfo(req.headers.userid);
    if (!userInfo || userInfo.UserID == 0 || userInfo.UserAccessToken != req.headers.token) {
      return false;
    } else {
      return true;
    }
  }

  /**
   * 判断用户在某个URL下是否有相关权限
   * @param Url
   * @param userRule 用户信息中的UserRule
   * @param partnersId 企业ID
   * @param Able 权限标记字符串 如：禁止删除
   * @returns {Promise<boolean>}
   */
  async checkAble(Url, userRule, partnersId, Able) {
    if (Url.indexOf("?") > 0) {
      Url = Url.split("?")[0];
    }
    var PageRule = await getPageRule(Url, userRule, partnersId);
    if (Able != "") {
      if (!PageRule || PageRule.A.indexOf(Able) > -1) {
        return false;
      }
    }
    return true;
  };
};